package com.cmcc.aregister.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.cache.CacheManager;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.FamilyModel;
import com.cmcc.aregister.cs.model.MemberModel;
import com.cmcc.aregister.cs.model.PayTypeModel;
import com.cmcc.aregister.cs.util.PotoalParcelable;
import com.cmcc.aregister.cs.util.Utils;
import com.cmcc.aregister.ui.adapter.AppointmentAdapter;
import com.cmcc.sdk.CmccDataStatistics;

/**
 * 预约挂号
 * 
 * @author ld
 * 
 */
public class AppointmentActivity extends ParentActivity
{
    private static final String TAG = "AppointmentActivity";
    // ------view------
    private LinearLayout two_top_hearder;
    private Button top_left_image;
    private Button top_right_voice_btn;
    private Button top_right_btn;
    // private TextView top_left, top_center, top_right;
    private LinearLayout user_layout;
    private TextView phonenumber;
    // private ImageView get_appointment;
    private ListView listview;
    // private TextView[] text_circle = new TextView[5];
    // private ImageView[] arrows = new ImageView[5];
    private LinearLayout appointment_view;
    private Button register_confirm;

    // ------parameter------
    private int currentTitleSelect = -1;
    private AppointmentAdapter local_appointmentAdapter;
    private AppointmentAdapter other_appointmentAdapter;
    private boolean hasTwoTop = true;

    private String unit_id = "";
    private String dep_id = "";
    private String doctor_id = "";
    private String schedule_id = "";
    private String schedule_info = "";
    private String detl_id = "";
    private String tiptype = "";
    private String guahao_amt = "";
    private String[][] value = { { "", "", "", "", "", "", "" },
        { "", "", "", "", "", "", "" } };
    private String area_id = "";
    private String unit_id_other = "";
    private String dep_id_other = "";
    private String doctor_id_other = "";
    private String reservationType = "";// 异地（普通/专家）
    private String schedule_id_other = "";
    /**
     * 异地预约地域名称（省市）
     */
    String other_pro_area_name = "";
    String unit_name_other = "";
    String dep_name_other = "";
    String doc_name_other = "";
    String sch_content_other = "";
    String outFee = ""; // 出诊费用
    private boolean isLocal = true;

    private String isAutoAppointment = "";
    private String isHaoyuanTip = "";

    private ArrayList<PayTypeModel> paytypeList;// 支付方式
    private List<MemberModel> members;
    private String params = "";

    /**
     * 异地预约的一些组件（专家，普通）
     */
    // private Button vip_btn,common_btn;

    // ------------
    /*
     * 组装说明： 某省（直辖市） 某地市xxxx（医院名称） xxx（科室名称） xxx（医生姓名） xxxx-xx-xx（就诊日期） 星期几
     * 上午(下午)， 以一个空格隔开。
     */
    private String orderDesc = "";// 异地预约提交订单的描述

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        isAutoAppointment = intent.getStringExtra("isAutoAppointment");
        isHaoyuanTip = intent.getStringExtra("isHaoyuanTip");
        init(R.layout.p_appointment);
        mHttpManager.setCxt(getParent());
    }

    @Override
    protected void setupData()
    {

        local_appointmentAdapter = new AppointmentAdapter(context, 4,
            isHaoyuanTip);
        other_appointmentAdapter = new AppointmentAdapter(context, 5,
            isHaoyuanTip);
        hasTwoTop = !getIntent().hasExtra(
            Arguments.ACTIVITY_JUMP_SCREENING_TYPE);
        if (getIntent().getParcelableExtra(Arguments.INTENT_TO_NEXT_PAGE) != null)
        {
            Map<String, Object> httpMap = ((PotoalParcelable) getIntent()
                .getParcelableExtra(Arguments.INTENT_TO_NEXT_PAGE)).map;
            int type = getIntent().getIntExtra(
                Arguments.ACTIVITY_JUMP_SCREENING_TYPE, 0);// 确定第一项，本地为医院，异地为城市
            String area_type = (String) httpMap
                .get(Arguments.ACTIVITY_JUMP_AREA_TYPE);
            if (ConfigUtilHelper.debug)
            {
                Log.i(TAG, "------type------" + type + "-area_type-"
                    + area_type);
            }
            String city_str = "";
            String hos_str = "";
            String dep_str = "";
            String doc_str = "";

            if ("1".equals(area_type))
            {// 1：异地,这部分暂时没用到20121204

                switch (type)
                {
                case 0:
                    city_str = "测试";
                    break;
                case 1:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    break;
                case 2:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    if (ConfigUtilHelper.debug)
                    {
                        Log.i(TAG, "------科室对应医院名称------" + hos_str);
                    }
                    dep_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DEP_ID);
                    dep_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT);
                    break;
                case 3:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    dep_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DEP_ID);
                    dep_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT);
                    schedule_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_SCHEDULE_ID);
                    schedule_info = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR);
                    doctor_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DOCTOR_ID);
                    doc_str = schedule_info;
                    guahao_amt = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_GUAHAO_AMT);
                    break;
                }

                currentTitleSelect = R.id.center_btn;
                other_appointmentAdapter.visibility = type;
                other_appointmentAdapter.content[0] = city_str;
                other_appointmentAdapter.content[1] = hos_str;
                other_appointmentAdapter.content[2] = dep_str;
                other_appointmentAdapter.content[3] = doc_str;
            }
            else
            {// 0：本地

                switch (type)
                {
                case 0:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    break;
                case 1:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    if (ConfigUtilHelper.debug)
                    {
                        Log.i(TAG, "------科室对应医院名称------" + hos_str);
                    }
                    dep_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DEP_ID);
                    dep_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT);
                    break;
                case 2:
                    unit_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID);
                    hos_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL);
                    dep_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DEP_ID);
                    dep_str = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT);
                    schedule_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_SCHEDULE_ID);
                    schedule_info = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR);
                    doctor_id = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_DOCTOR_ID);
                    doc_str = schedule_info;
                    guahao_amt = (String) httpMap
                        .get(Arguments.ACTIVITY_JUMP_GUAHAO_AMT);
                    break;
                }

                currentTitleSelect = R.id.left_btn;
                local_appointmentAdapter.visibility = type;
                local_appointmentAdapter.content[0] = hos_str;
                local_appointmentAdapter.content[1] = dep_str;
                local_appointmentAdapter.content[2] = doc_str;
            }

        }
    }

    @Override
    protected void setupView()
    {
        // ((TextView) findViewById(R.id.title))
        // .setText(R.string.appointment_register);

        // 异地专家和普通按钮初始化
        // vip_btn = (Button) findViewById(R.id.vip_btn);
        // common_btn = (Button) findViewById(R.id.common_btn);
        // vip_btn.setOnClickListener(onClickListener);
        // common_btn.setOnClickListener(onClickListener);
        //
        two_top_hearder = (LinearLayout) findViewById(R.id.two_top_hearder);
        top_left_image = (Button) findViewById(R.id.top_left_image);
        top_right_btn = (Button) findViewById(R.id.top_right_btn);
        top_right_voice_btn = (Button) findViewById(R.id.top_right_voice_btn);
        top_right_voice_btn.setVisibility(View.VISIBLE);
        top_right_voice_btn.setOnClickListener(onClickListener);
        top_right_btn.setVisibility(View.GONE);
        ImageView top_left = (ImageView) findViewById(R.id.left_btn);
        ImageView top_center = (ImageView) findViewById(R.id.center_btn);
        ImageView top_right = (ImageView) findViewById(R.id.right_btn);
        user_layout = (LinearLayout) findViewById(R.id.user_layout);
        phonenumber = (TextView) findViewById(R.id.phonenumber);
        // get_appointment = (ImageView) findViewById(R.id.get_appointment);
        // text_circle[0] = (TextView) findViewById(R.id.first);
        // text_circle[1] = (TextView) findViewById(R.id.second);
        // text_circle[2] = (TextView) findViewById(R.id.third);
        // text_circle[3] = (TextView) findViewById(R.id.fouth);
        // text_circle[4] = (TextView) findViewById(R.id.five);
        // text_circle[5] = (TextView) findViewById(R.id.six);
        // arrows[0] = (ImageView) findViewById(R.id.first_arrows);
        // arrows[1] = (ImageView) findViewById(R.id.second_arrows);
        // arrows[2] = (ImageView) findViewById(R.id.third_arrows);
        // arrows[3] = (ImageView) findViewById(R.id.fouth_arrows);
        // arrows[4] = (ImageView) findViewById(R.id.five_arrows);
        // arrows[5] = (ImageView) findViewById(R.id.six_arrows);
        appointment_view = (LinearLayout) findViewById(R.id.appointment_view);
        listview = (ListView) findViewById(R.id.listview);
        register_confirm = (Button) findViewById(R.id.register_confirm);

        if (hasTwoTop)
        {
            two_top_hearder.setVisibility(View.VISIBLE);
            user_layout.setVisibility(View.VISIBLE);
            check(R.id.left_btn, false);
            top_left_image.setVisibility(View.VISIBLE);
            top_right_btn.setVisibility(View.GONE);
            top_right_btn.setText(R.string.fast_appointment);
            top_left_image.setOnClickListener(onClickListener);
            top_right_btn.setOnClickListener(onClickListener);
            top_left.setOnClickListener(onClickListener);
            top_center.setOnClickListener(onClickListener);
            top_right.setOnClickListener(onClickListener);
            // get_appointment.setOnClickListener(onClickListener);
            listview.setAdapter(local_appointmentAdapter);
            // text_circle[4].setVisibility(View.GONE);
            // arrows[4].setVisibility(View.GONE);
        }
        else
        {
            two_top_hearder.setVisibility(View.GONE);
            user_layout.setVisibility(View.GONE);
            if (currentTitleSelect == R.id.left_btn)
            {
                listview.setAdapter(local_appointmentAdapter);
                // text_circle[4].setVisibility(View.GONE);
                // arrows[4].setVisibility(View.GONE);
            }
            else
            {
                listview.setAdapter(other_appointmentAdapter);
                // listview.setAdapter(other_appointmentAdapter);
                // text_circle[4].setVisibility(View.VISIBLE);
                // arrows[4].setVisibility(View.VISIBLE);
            }
        }
        register_confirm.setOnClickListener(onClickListener);
        listview.setOnItemClickListener(onItemClickListener);
    }

    public void check(int id, boolean hasAnimation)
    {
        if (currentTitleSelect == id)
        {
            return;
        }
        if (currentTitleSelect > 0)
        {
            findViewById(currentTitleSelect).setSelected(false);
        }
        currentTitleSelect = id;
        findViewById(currentTitleSelect).setSelected(true);
        if (hasAnimation)
        {
            start3DAnimation(appointment_view);
        }
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v)
        {
            int id = v.getId();
            switch (id)
            {
            case R.id.top_left_image:
                Intent intent = new Intent(AppointmentActivity.this,
                    NewIndexActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
                break;
            case R.id.top_right_btn:
                if (!Utils.isLogin(context))
                {
                    Intent intent_1 = new Intent(AppointmentActivity.this,
                        UserLoginActivity.class);
                    intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_1);
                }
                else
                {
                    Intent intent_1 = new Intent(AppointmentActivity.this,
                        OrderDetailActivity.class);
                    intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_1);
                }
                break;
            case R.id.top_right_voice_btn:
                if (!Utils.isLogin(context))
                {
                    Intent intent_1 = new Intent(AppointmentActivity.this,
                        UserLoginActivity.class);
                    intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_1);
                }
                else
                {
                    Intent intent_1 = new Intent(AppointmentActivity.this,
                        NewVoiceActivity.class);
                    intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_1);
                }
                break;
            case R.id.right_btn:
                if (!Utils.isLogin(context))
                {
                    Intent intent_1 = new Intent(AppointmentActivity.this,
                        UserLoginActivity.class);
                    intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_1);
                }
                else
                {
                    Intent intent_2 = new Intent(AppointmentActivity.this,
                        AppManagerActivity.class);
                    boolean isLocal = true;
                    if (currentTitleSelect == R.id.center_btn)
                    {
                        isLocal = false;
                    }
                    else
                    {
                        isLocal = true;
                    }
                    intent_2.putExtra("isLocal", isLocal);
                    intent_2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent_2);
                }
                break;
            case R.id.left_btn:
                check(id, true);
                break;
            case R.id.center_btn:
                // showInfoDialog(getString(R.string.yidi_no_use), true);
                CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                    "c_other");
                check(id, true);// 异地预约功能启动的时候用
                break;
            case R.id.register_confirm:
                if (!ConfigUtilHelper.isLogin(context))
                {
                    gotoLogin();
                    return;
                }
                registerConfirm();
                break;
            }
        }
    };

    private void gotoLogin()
    {
        Intent intent = new Intent(AppointmentActivity.this,
            UserLoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    /**
     * 提交预约
     */
    private void registerConfirm()
    {
        if (isHaoyuanTip != null && isHaoyuanTip.equals("yes"))
        {
            if (currentTitleSelect == R.id.left_btn && "".equals(tiptype))
            {

                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.please_choice_tiptype),
                    getString(R.string.close));
            }
            sendHaoyuanTipTask();
        }
        else
        {
            if (currentTitleSelect == R.id.left_btn && "".equals(detl_id))
            {
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.choice_complete),
                    getString(R.string.close));
            }
            else if (currentTitleSelect == R.id.center_btn
                && "".equals(schedule_id_other))
            {
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.choice_complete),
                    getString(R.string.close));
            }
            else if (currentTitleSelect == R.id.left_btn && !"".equals(detl_id))
            {

                getHospitalPayTypeTask(unit_id);
            }
            else if (currentTitleSelect == R.id.center_btn
                && !"".equals(schedule_id_other))
            {
                isLocal = false;
                orderDesc = other_pro_area_name + " " + unit_name_other + " "
                    + dep_name_other + " " + doc_name_other + " "
                    + sch_content_other;
                // String[] desc = orderDesc.split(" ");
                getFamily();
            }
            else
            {
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.no_left_num), getString(R.string.close));
            }
        }
    }

    /**
     * 添加号源提醒任务
     */
    private HttpAsyncTask sendHaoyuanTipTask;

    private void sendHaoyuanTipTask()
    {
        logger.d("----------sendHaoyuanTipTask----------");
        JSONObject obj = new JSONObject();
        try
        {
            obj.put("request_type", Arguments.AddRemindTaskRemoting);
            obj.put("user_mobile", ConfigUtilHelper.getMobile(app));
            obj.put("channel", "medical_client_android");
            obj.put("version",
                ConfigUtilHelper.getVersionName(getApplication()));
            obj.put("unit_id", unit_id);
            obj.put("dep_id", dep_id);
            obj.put("doc_id", doctor_id);
            obj.put("remind_type", tiptype);
            String str[] = local_appointmentAdapter.content[2].split(" ");
            String doctor_name = str[0];
            obj.put("unit_name", local_appointmentAdapter.content[0]);
            obj.put("dep_name", local_appointmentAdapter.content[1]);
            obj.put("doc_name", doctor_name);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        params = obj.toString();
        String url = Arguments.GET_AUTO_APPOINT_SERVICE_URL;
        sendHaoyuanTipTask = new HttpAsyncTask(
            GlobalSetting.XML_ADD_HAOYUAN_TIP_TASK, url, params,
            HttpAsyncTask.CACHE_TYPE_ACTION, true, true);
        showProgressDialogNew(R.string.dialog_loading_generatal_task);
        sendHaoyuanTipTask.setUIHandler(sendHaoyuanTipTaskHandler);
        mHttpManager.execute(sendHaoyuanTipTask);
    }

    Handler sendHaoyuanTipTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            logger.d("----------sendHaoyuanTipTaskHandler----------");
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null)
                {
                    Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
                    String message = (String) httpMap.get(Arguments.RESULT_MSG);
                    String resultCode = (String) httpMap
                        .get(Arguments.RESULT_CODE);
                    String taskId = (String) httpMap.get(Arguments.TASKID);
                    String phone = (String) httpMap.get(Arguments.PHONE);
                    String hosName = (String) httpMap.get(Arguments.HOS_NAME);
                    String depName = (String) httpMap.get(Arguments.DEP_NAME);
                    String docName = (String) httpMap.get(Arguments.DOC_NAME);
                    String startTime = (String) httpMap
                        .get(Arguments.APPOINT_START_TIME);
                    String remindType = (String) httpMap
                        .get(Arguments.REMIND_TYPE);
                    // 如果添加成功且为放好时间提醒，保存到缓存中
                    if (resultCode.equals("0") && remindType.equals("1"))
                    {
                        CacheManager.getInstance().saveHaoyuanTipCache(phone,
                            startTime, hosName, depName, docName, taskId);
                    }
                    _showInfoDialog(message,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                int which)
                            {
                                Intent intent = new Intent(
                                    AppointmentActivity.this,
                                    HaoyuanTipActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                            }
                        });
                }
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.read_fail), getString(R.string.close));
                break;
            }
        }
    };

    /**
     * 获取医院的支付方式
     */
    private HttpAsyncTask getHospitalPayTypeTask;

    /**
     * 根据医院的ID获取支付方式
     * 
     * @param unit_id
     */
    private void getHospitalPayTypeTask(String unit_id)
    {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("unit_id", unit_id);
        parameterMap.put("mobile", ConfigUtilHelper.getMobile(app));
        String changeUserInfoUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_HOSPITAL_PAY_TYPE, parameterMap);
        getHospitalPayTypeTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_HOSPITAL_PAY_TYPE_INFO, changeUserInfoUrl,
            HttpAsyncTask.CACHE_TYPE_ACTION, false);
        showProgressDialogNew(R.string.get_pay_type_tips);
        getHospitalPayTypeTask.setUIHandler(getHospitalPayTypeTaskHandler);
        mHttpManager.execute(getHospitalPayTypeTask);
    }

    Handler getHospitalPayTypeTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                if (msg.obj != null)
                {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    paytypeList = (ArrayList<PayTypeModel>) map
                        .get(Arguments.XML_PAYTYPE_LIST);
                    isLocal = true;
                    getFamily();
                }
                else
                {
                    dismissProgressDialog();
                }
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.read_fail), getString(R.string.close));
                break;
            }
        };
    };

    private void getFamily()
    {
        showProgressDialog();
        String params = "cid=8&token=wxcityMobileClient&phone="
            + ConfigUtilHelper.getMobile(app);
        sendGetFamilyTask = new HttpAsyncTask(HttpAsyncTask.POST,
            GlobalSetting.XML_GET_FAMILY_INFO, Arguments.GET_FAMILY_INFO_URL,
            params, HttpAsyncTask.CACHE_TYPE_ACTION, true);
        sendGetFamilyTask.setUIHandler(sendGetFamilyTaskHandler);
        mHttpManager.execute(sendGetFamilyTask);
    }

    /** 获取家庭成员信息 */
    private HttpAsyncTask sendGetFamilyTask;

    private Handler sendGetFamilyTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null)
                {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    FamilyModel family = (FamilyModel) map
                        .get(Arguments.XML_FAMILY_INFO);
                    if (family != null && family.members != null
                        && family.members.size() > 0)
                    {
                        members = family.members;
                    }
                    if (isLocal)
                    {
                        Intent intent_1 = new Intent(AppointmentActivity.this,
                            OrderDetailActivity.class);

                        CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                            "sure_order");
                        Map<String, Object> httpMap = new HashMap<String, Object>();
                        httpMap.put(Arguments.ACTIVITY_JUMP_UNIT_ID, unit_id);
                        httpMap.put(Arguments.ACTIVITY_JUMP_DETL_ID, detl_id);
                        httpMap.put(Arguments.ACTIVITY_JUMP_ORDER_DETAIL,
                            local_appointmentAdapter.content);
                        httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE,
                            currentTitleSelect);
                        httpMap.put(Arguments.ACTIVITY_JUMP_GUAHAO_AMT,
                            guahao_amt);
                        httpMap.put(Arguments.MEMBER_LIST, members);
                        httpMap.put(Arguments.ACTIVITY_JUMP_PAY_TYPE_LIST,
                            paytypeList);
                        httpMap.put(Arguments.ACTIVITY_JUMP_SCHEDULE_ID,
                            schedule_id);
                        PotoalParcelable potoal = new PotoalParcelable(httpMap);
                        intent_1.putExtra("isAutoAppointment",
                            isAutoAppointment);
                        intent_1
                            .putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                        intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent_1);
                    }
                    else
                    {
                        Intent intent_1 = new Intent(AppointmentActivity.this,
                            OrderDetailActivity.class);
                        Map<String, Object> httpMap = new HashMap<String, Object>();
                        httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE,
                            currentTitleSelect);

                        httpMap.put(Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR_TYPE,
                            reservationType);
                        httpMap.put(Arguments.ACTIVITY_JUMP_DEP_ID,
                            dep_id_other);
                        httpMap.put(Arguments.ACTIVITY_JUMP_DOCTOR_ID,
                            doctor_id_other);

                        httpMap.put(Arguments.ACTIVITY_JUMP_UNIT_ID,
                            unit_id_other);
                        httpMap.put(Arguments.ACTIVITY_JUMP_DETL_ID,
                            schedule_id_other);
                        httpMap.put(Arguments.ACTIVITY_JUMP_ORDER_DETAIL,
                            orderDesc);
                        httpMap.put(Arguments.ACTIVITY_JUMP_GUAHAO_AMT, outFee);
                        httpMap.put(Arguments.MEMBER_LIST, members);
                        PotoalParcelable potoal = new PotoalParcelable(httpMap);
                        intent_1
                            .putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                        intent_1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent_1);
                    }

                }
                else
                {
                    showMsg(R.string.read_member_fail);
                }
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.get_family_info_err),
                    getString(R.string.close));
                break;
            }
        };
    };

    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position,
            long arg3)
        {
            if (currentTitleSelect == R.id.left_btn)
            {
                switch (position)
                {
                case 0:
                    CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                        "c_hos");
                    break;
                case 1:
                    CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                        "c_dep");
                    break;
                case 2:
                    CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                        "c_doc");
                    break;
                case 3:
                    CmccDataStatistics.onEvent(AppointmentActivity.this, 0,
                        "c_time");
                    break;
                default:
                    break;
                }
                if (position == 3 && "".equals(schedule_id))
                {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.please_choice_doctort),
                        getString(R.string.close));
                }
                else
                {
                    gotoChoiceActivity(position);
                }
            }
            else
            {// 异地板块选择
                if (position == 4 && "".equals(doctor_id_other))
                {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.please_choice_doctort),
                        getString(R.string.close));
                }
                else if (position == 3 && "".equals(dep_id_other))
                {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.please_choice_dep),
                        getString(R.string.close));

                }
                else if (position == 2 && "".equals(unit_id_other))
                {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.please_choice_hospital),
                        getString(R.string.close));
                }
                else if (position == 1 && "".equals(area_id))
                {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.please_choice_area),
                        getString(R.string.close));
                }
                else
                {
                    gotoChoiceActivity_other(position);
                }
            }

        }
    };

    /**
     * 跳转至选择界面（本地）
     * 
     * @param position
     */
    private void gotoChoiceActivity(int position)
    {
        Intent intent = new Intent(AppointmentActivity.this,
            ChoiceActivity.class);
        Map<String, Object> httpMap = new HashMap<String, Object>();
        httpMap.put(Arguments.ACTIVITY_JUMP_CHOICE_TYPE, position);
        httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE, currentTitleSelect);
        httpMap.put(Arguments.ACTIVITY_JUMP_UNIT_ID, unit_id);
        httpMap.put(Arguments.ACTIVITY_JUMP_DEP_ID, dep_id);
        httpMap.put(Arguments.ACTIVITY_JUMP_DOCTOR_ID, doctor_id);
        httpMap.put(Arguments.ACTIVITY_JUMP_SCHEDULE_ID, schedule_id);
        httpMap.put(Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR, schedule_info);
        PotoalParcelable potoal = new PotoalParcelable(httpMap);
        intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
        intent.putExtra("isHaoyuanTip", isHaoyuanTip);
        intent.putExtra("isAutoAppointment", isAutoAppointment);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, Arguments.ACTIVITY_REQUESTCODE);
    }

    /**
     * 跳转至选择界面（异地）
     * 
     * @param position
     */
    private void gotoChoiceActivity_other(int position)
    {
        Intent intent = new Intent(AppointmentActivity.this,
            ChoiceActivity.class);
        Map<String, Object> httpMap = new HashMap<String, Object>();
        httpMap.put(Arguments.ACTIVITY_JUMP_CHOICE_TYPE, position);// 异地选择第几行
        httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE, currentTitleSelect);// 异地表示（R.id.left_btn为本地，R.id.right_btn为异地）
        httpMap.put(Arguments.ACTIVITY_JUMP_OTHER_AREAR_ID, area_id);
        httpMap.put(Arguments.ACTIVITY_JUMP_UNIT_ID, unit_id_other);
        httpMap.put(Arguments.ACTIVITY_JUMP_DEP_ID, dep_id_other);
        httpMap.put(Arguments.ACTIVITY_JUMP_DOCTOR_ID, doctor_id_other);
        httpMap.put(Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR, schedule_info);
        PotoalParcelable potoal = new PotoalParcelable(httpMap);
        intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, Arguments.ACTIVITY_REQUESTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            Map<String, Object> httpMap = ((PotoalParcelable) data
                .getParcelableExtra(Arguments.INTENT_TO_NEXT_PAGE)).map;
            int position = (Integer) httpMap
                .get(Arguments.ACTIVITY_JUMP_SCREENING_TYPE);
            AppointmentAdapter appointmentAdapter = null;
            if (currentTitleSelect == R.id.left_btn)
            {// 本地
                appointmentAdapter = local_appointmentAdapter;
                switch (position)
                {
                case 3:
                    if (isHaoyuanTip != null && isHaoyuanTip.equals("yes"))
                    {
                        appointmentAdapter.content[3] = httpMap.get(
                            Arguments.ACTIVITY_JUMP_CHOICE_TIPTYPE_VALUE)
                            .toString();
                        tiptype = httpMap.get(
                            Arguments.ACTIVITY_JUMP_CHOICE_TIPTYPE).toString();
                    }
                    else
                    {
                        appointmentAdapter.content[3] = httpMap.get(
                            Arguments.ACTIVITY_JUMP_CHOICE_TIME).toString();
                        detl_id = httpMap.get(Arguments.ACTIVITY_JUMP_DETL_ID)
                            .toString();
                    }
                    break;
                case 2:
                    clearValue();
                    schedule_info = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR).toString();
                    appointmentAdapter.content[2] = schedule_info;
                    doctor_id = httpMap.get(Arguments.ACTIVITY_JUMP_DOCTOR_ID)
                        .toString();
                    if (currentTitleSelect == R.id.left_btn)
                    {
                        schedule_id = httpMap.get(
                            Arguments.ACTIVITY_JUMP_SCHEDULE_ID).toString();
                        guahao_amt = httpMap.get(
                            Arguments.ACTIVITY_JUMP_GUAHAO_AMT).toString();
                    }
                    else
                    {
                        doctor_id = httpMap.get(
                            Arguments.ACTIVITY_JUMP_DOCTOR_ID).toString();
                    }
                    appointmentAdapter.content[1] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT).toString();
                    dep_id = httpMap.get(Arguments.ACTIVITY_JUMP_DEP_ID)
                        .toString();
                    appointmentAdapter.content[0] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL).toString();
                    unit_id = httpMap.get(Arguments.ACTIVITY_JUMP_UNIT_ID)
                        .toString();
                    break;
                case 1:
                    clearValue();
                    appointmentAdapter.content[1] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT).toString();
                    dep_id = httpMap.get(Arguments.ACTIVITY_JUMP_DEP_ID)
                        .toString();
                    appointmentAdapter.content[0] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL).toString();
                    unit_id = httpMap.get(Arguments.ACTIVITY_JUMP_UNIT_ID)
                        .toString();
                    break;
                case 0:
                    clearValue();
                    appointmentAdapter.content[0] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL).toString();
                    unit_id = httpMap.get(Arguments.ACTIVITY_JUMP_UNIT_ID)
                        .toString();
                    break;
                }
                appointmentAdapter.visibility = position;
                appointmentAdapter.notifyDataSetChanged();
            }
            else
            {// 异地
                appointmentAdapter = other_appointmentAdapter;
                switch (position)
                {
                case 4:
                    sch_content_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_OTHER_SCH_CONTENT).toString();
                    appointmentAdapter.content[4] = sch_content_other;
                    schedule_id_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_OTHER_SCH_ID).toString();
                    break;
                case 3:
                    clearValue_other_03();

                    outFee = httpMap.get(Arguments.ACTIVITY_JUMP_GUAHAO_AMT)
                        .toString();
                    doc_name_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR).toString();
                    appointmentAdapter.content[3] = doc_name_other;
                    doctor_id_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_DOCTOR_ID).toString();
                    reservationType = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DOCTOR_TYPE).toString();
                    break;
                case 2:
                    clearValue_other_02();
                    dep_name_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_DEPARTMENT).toString();
                    appointmentAdapter.content[2] = dep_name_other;
                    dep_id_other = httpMap.get(Arguments.ACTIVITY_JUMP_DEP_ID)
                        .toString();
                    // appointmentAdapter.content[0] = httpMap.get(
                    // Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL).toString();
                    // unit_id = httpMap.get(Arguments.ACTIVITY_JUMP_UNIT_ID)
                    // .toString();
                    break;
                case 1:
                    clearValue_other_01();
                    unit_name_other = httpMap.get(
                        Arguments.ACTIVITY_JUMP_CHOICE_HOSPITAL).toString();
                    ;
                    appointmentAdapter.content[1] = unit_name_other;
                    unit_id_other = httpMap
                        .get(Arguments.ACTIVITY_JUMP_UNIT_ID).toString();
                    break;
                case 0:
                    clearValue_other();
                    appointmentAdapter.content[0] = httpMap.get(
                        Arguments.ACTIVITY_JUMP_OTHER_AREAR_NAME).toString();
                    area_id = httpMap.get(
                        Arguments.ACTIVITY_JUMP_OTHER_AREAR_ID).toString();
                    other_pro_area_name = httpMap.get(
                        Arguments.ACTIVITY_JUMP_OTHER_PRO_NAME).toString()
                        + " "
                        + httpMap.get(Arguments.ACTIVITY_JUMP_OTHER_AREAR_NAME)
                            .toString();
                    break;
                }
                appointmentAdapter.visibility = position;
                appointmentAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void animFrushView()
    {
        switch (currentTitleSelect)
        {
        case R.id.left_btn:
            listview.setAdapter(local_appointmentAdapter);
            // text_circle[4].setVisibility(View.GONE);
            // arrows[4].setVisibility(View.GONE);
            saveValue(1);
            setValue(value[0]);
            break;
        case R.id.center_btn:
            listview.setAdapter(other_appointmentAdapter);
            // text_circle[4].setVisibility(View.VISIBLE);
            // arrows[4].setVisibility(View.VISIBLE);
            saveValue(0);
            setValue(value[1]);
            break;
        }
    }

    public void saveValue(int type)
    {
        value[type][0] = unit_id;
        value[type][1] = dep_id;
        value[type][2] = doctor_id;
        value[type][3] = schedule_id;
        value[type][4] = schedule_info;
        value[type][5] = detl_id;
        value[type][6] = guahao_amt;
    }

    public void setValue(String[] str)
    {
        unit_id = str[0];
        dep_id = str[1];
        doctor_id = str[2];
        schedule_id = str[3];
        schedule_info = str[4];
        detl_id = str[5];
        guahao_amt = str[6];
    }

    public void clearValue()
    {
        unit_id = "";
        dep_id = "";
        doctor_id = "";
        schedule_id = "";
        schedule_info = "";
        detl_id = "";
        guahao_amt = "";
    }

    public void clearValue_other()
    {
        area_id = "";
        unit_id_other = "";
        dep_id_other = "";
        doctor_id_other = "";
        schedule_id_other = "";

        other_pro_area_name = "";
        unit_name_other = "";
        dep_name_other = "";
        doc_name_other = "";
        sch_content_other = "";
        reservationType = "";

    }

    public void clearValue_other_01()
    {
        unit_id_other = "";
        dep_id_other = "";
        doctor_id_other = "";
        reservationType = "";
        schedule_id_other = "";

        unit_name_other = "";
        dep_name_other = "";
        doc_name_other = "";
        sch_content_other = "";

    }

    public void clearValue_other_02()
    {
        dep_id_other = "";
        doctor_id_other = "";
        reservationType = "";
        schedule_id_other = "";

        dep_name_other = "";
        doc_name_other = "";
        sch_content_other = "";

    }

    public void clearValue_other_03()
    {
        doctor_id_other = "";
        schedule_id_other = "";
        reservationType = "";

        doc_name_other = "";
        sch_content_other = "";

    }

    /**
     * 判断提示注册
     */
    private void showRegister()
    {
        if (ConfigUtilHelper.isLogin(app)
            && ConfigUtilHelper.getUsername(app).equals(""))
        {
            final Dialog dialog = new Dialog(this, R.style.custom_dialog);
            dialog.setContentView(R.layout.dialog_double_alert);
            ((TextView) dialog.findViewById(R.id.dialog_content))
                .setText(R.string.dialog_no_register1);
            ((Button) dialog.findViewById(R.id.left_btn))
                .setText(R.string.dialog_back_register);
            ((Button) dialog.findViewById(R.id.right_btn))
                .setText(R.string.dialog_sure_register);
            dialog.findViewById(R.id.left_btn).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        dialog.cancel();
                    }
                });
            dialog.findViewById(R.id.right_btn).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        doPWork_();
                    }
                });
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        showRegister();
        if (hasTwoTop)
        {
            if (!ConfigUtilHelper.isLogin(app))
            {
                phonenumber.setText(R.string.login_first_info);
                // get_appointment.setVisibility(View.GONE);
            }
            else
            {
                if (ConfigUtilHelper.getUsername(app).equals(""))
                {
                    phonenumber.setText(ConfigUtilHelper.getMobile(app));
                }
                else
                {
                    phonenumber.setText(ConfigUtilHelper.getUsername(app));
                }
                // get_appointment.setVisibility(View.VISIBLE);
            }
        }
        if (app.confirm_order_success)
        {
            app.confirm_order_success = false;
            if (currentTitleSelect == R.id.left_btn)
            {
                clearValue();
                local_appointmentAdapter.visibility = -1;
                local_appointmentAdapter.notifyDataSetChanged();
            }
            else
            {
                clearValue_other();
                other_appointmentAdapter.visibility = -1;
                other_appointmentAdapter.notifyDataSetChanged();
            }

        }

        // 如果是自动预约过来的
        if (isAutoAppointment != null && isAutoAppointment.equals("yes"))
        {
            ((ImageView) findViewById(R.id.title))
                .setBackgroundResource(R.drawable.add_zidongyuyue);
            top_left_image.setVisibility(View.GONE);
            top_right_voice_btn.setVisibility(View.GONE);
            two_top_hearder.setVisibility(View.GONE);
            // get_appointment.setVisibility(View.GONE);
            user_layout.setVisibility(View.GONE);
        }

        // 如果是号源提醒过来的
        if (isHaoyuanTip != null && isHaoyuanTip.equals("yes"))
        {
            ((ImageView) findViewById(R.id.title))
                .setBackgroundResource(R.drawable.add_haoyuantixing);
            top_left_image.setVisibility(View.GONE);
            top_right_voice_btn.setVisibility(View.GONE);
            two_top_hearder.setVisibility(View.GONE);
            // get_appointment.setVisibility(View.GONE);
            user_layout.setVisibility(View.GONE);
            register_confirm.setText("提交任务");
        }
    }

    @Override
    public void doPWork_()
    {
        // TODO Auto-generated method stub
        super.doPWork_();
        gotoRegist();
    }

    @Override
    public void doBackIndex()
    {
        super.doBackIndex();
        gotoIndex();
    }

    private void gotoIndex()
    {
        finish();
        /*
         * Intent intent = new Intent(AppointmentActivity.this,
         * NewIndexActivity.class);
         * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         * startActivity(intent);
         */
    }

    /**
     * 去注册
     */
    private void gotoRegist()
    {
        Intent intent = new Intent(AppointmentActivity.this,
            UserRegisterActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("mobile", ConfigUtilHelper.getMobile(app));
        startActivity(intent);
        finish();
    }
}